﻿using System;
using AutoTaskSchedule.Core.Enums;
using AutoTaskSchedule.Core.Models;
using FluentScheduler;

namespace AutoTaskSchedule.Core.Business
{
    public class TimingTaskFactory : TaskFactory
    {
        public TimingTaskFactory(Task task)
            : base(task)
        {
        }

        public override void Create()
        {
            Action<Schedule> action = null;

            var option = new TimingOption();

            #region action

            switch (option.RunEvery)
            {
                case RunEvery.每个工作日:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weekdays()
                                .At(option.Hour.Value, option.Minute.Value);
                    break;

                case RunEvery.每周:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .At(option.Hour.Value, option.Minute.Value);
                    break;

                case RunEvery.每年最后一天:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Years()
                                .OnTheLastDay();

                    break;

                case RunEvery.每天:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Days()
                                .At(option.Hour.Value, option.Minute.Value);

                    break;

                case RunEvery.每小时:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Hours()
                                .At(option.Minute.Value);

                    break;

                case RunEvery.每月第一天:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Months()
                                .On(1)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;

                case RunEvery.每月最后一天:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Months()
                                .OnTheLastDay()
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
                case RunEvery.每周一:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .On(DayOfWeek.Monday)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
                case RunEvery.每周二:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .On(DayOfWeek.Tuesday)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
                case RunEvery.每周三:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .On(DayOfWeek.Wednesday)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
                case RunEvery.每周四:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .On(DayOfWeek.Thursday)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
                case RunEvery.每周五:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .On(DayOfWeek.Friday)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
                case RunEvery.每周六:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .On(DayOfWeek.Saturday)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
                case RunEvery.每周天:
                    action =
                        schedule =>
                            schedule.WithName(_task.Name)
                                .NonReentrant()
                                .ToRunEvery(1)
                                .Weeks()
                                .On(DayOfWeek.Sunday)
                                .At(option.Hour.Value, option.Minute.Value);

                    break;
            }
            #endregion

            if (action != null)
            {
                var autoTaskSchedule = GetAutoTaskSchedule();
                JobManager.AddJob(() => autoTaskSchedule.Execute(), shchedule => action.Invoke(shchedule));
            }
        }
    }
}